package controllers

import (
	"github.com/astaxie/beego"
	"hello/lib/mysqlLib"
	"log"
)

type UnbandController struct {
	beego.Controller
	PublicController
}

func (uc *UnbandController) Get() {
	gatewayID := uc.GetString("gateway_id")
	sqlStr := "select gateway_id from hh_gateway_band_RT where gateway_id = ?;"
	result, err := mysqlLib.Query(sqlStr, gatewayID)
	if err != nil {
		log.Printf("query gateway state error :%s",err.Error())
	}
	if len(result) == 0 {
		log.Printf("there is no gateway")
		unbandresponse := Response{}
		unbandresponse.Status = "error"
		unbandresponse.Errmsg = "not band"
		uc.Data["json"] = &unbandresponse
		uc.ServeJSON()
		//uc.response("error", "not band")
	}
	sqlStr = "delete from hh_gateway_band_RT where gateway_id = ?;"

	err = mysqlLib.Exec(sqlStr, gatewayID)
	if err != nil {
		log.Printf("unband device error :%s", err.Error())
		unbandresponse := Response{}
		unbandresponse.Status = "error"
		unbandresponse.Errmsg = "unband device error :" + err.Error()
		uc.Data["json"] = &unbandresponse
		uc.ServeJSON()
		//uc.response("error", "unband device error :"+err.Error())
	}
	unbandresponse := Response{}
	unbandresponse.Status = "ok"
	unbandresponse.Errmsg = "success"
	uc.Data["json"] = &unbandresponse
	uc.ServeJSON()
	//uc.response("ok", "success")
}
